using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;

namespace Super
{
	/// <summary>
	/// 所有On开头的函数原则上都不允许手动调用
	/// 页面第一次打开执行顺序：OnInit>>OnOpen>>OnUpdateData>>OnRefresh
	///
	/// UpdateData		更新页面的数据
	/// Refresh			刷新页面使用方法
	///	UpdateAndRefresh	更新数据和刷新页面
	///
	/// OnInit			只在页面初始化时执行，一切方法前执行，*只执行一次*
	/// OnOpen			每次页面打开时都会调用此方法
	/// OnClose			每次页面关闭时时调用
	/// OnUpdateData	第一次打开以及Refresh调用是执行
	/// </summary>
	public class UIBase : MonoBehaviour
	{
		private bool isInitd = false;

		[HideInInspector]
		public bool isOpened = false;
		public Action closeCallback;
		public List<WidgetProxy> widgetProxys = new List<WidgetProxy>();

		public void Awake()
		{
			OnDirtyRefresh();
		}
		public void Init()
		{
			BeforeInit();
			OnInit();
			AfterInit();
		}
		public void Open(params object[] args)
		{
			if (!CheckIfInit())
			{
				Init();
			}

			BeforeOpen(args);
			OnOpen(args);
			AfterOpen(args);
		}
		public void Close()
		{
			BeforeClose();
			OnClose();
			AfterClose();
		}
		
		public void UpdateData()
		{
			BeforeUpdateData();
			OnUpdateData();
			AfterUpdateData();
		}
		public void Refresh()
		{
			BeforeRefresh();
			OnRefresh();
			AfterRefresh();
		}

		public virtual void BeforeInit() { }
		public virtual void OnInit() { }
		public virtual void AfterInit()
		{
			isInitd = true;
		}
		public virtual void BeforeOpen(params object[] args) { }
		public virtual void OnOpen(params object[]args) { }
		public virtual void AfterOpen(params object[] args)
		{
			UpdateAndRefresh();
		}

		public virtual void BeforeClose(){ }
		public virtual void OnClose() { }
		public virtual void AfterClose()
		{
			EventManager.UnRegisterAll(this);
			TimerManager.UnRegisterAll(this);

			for (int i = 0; i < widgetProxys.Count; i++)
			{
				widgetProxys[i].Close();
			}
			gameObject.SetActive(false);
			isOpened = false;
			closeCallback?.Invoke();
			closeCallback = null;
		}

		public virtual void BeforeUpdateData() { }
		public virtual void OnUpdateData() { }
		public virtual void AfterUpdateData() { }

		public virtual void BeforeRefresh() { }
		public virtual void OnRefresh() { }
		public virtual void AfterRefresh()
		{
			isOpened = true;
		}

		private bool CheckIfInit()
		{
			return isInitd;
		}

		public bool CheckIfOpened()
		{
			return isOpened;
		}
		
		public void SetCloseCallback(Action callback)
		{
			closeCallback = callback;
		}

		public virtual void OnDirtyRefresh()
		{

		}


		/********************通用函数****************/
		public WidgetProxy NewWidgetProxy<T>(string path) where T : WidgetBase
		{

			WidgetProxy proxy = new WidgetProxy(typeof(T), transform.Find(path));
			widgetProxys.Add(proxy);
			return proxy;
		}

		public WidgetProxy NewWidgetProxy<T>(Transform parent, string path = "") where T : WidgetBase
		{
			WidgetProxy proxy = new WidgetProxy(typeof(T), parent.Find(path));
			widgetProxys.Add(proxy);
			return proxy;
		}

		public WidgetProxy NewWidgetProxy(Type widgetType, Transform parent, string path = "")
		{
			WidgetProxy proxy = new WidgetProxy(widgetType, parent.Find(path));
			widgetProxys.Add(proxy);
			return proxy;
		}

		public void UpdateAndRefresh()
		{
			UpdateData();
			Refresh();
		}
	}
}
